package com.iplant.pdm.controller.fpc;

import com.iplant.base.po.Pagination;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.basic.controller.BasicController;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.pdm.service.FPCService;
import com.iplant.pdm.service.po.fpc.FPCCapacity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/FPCCapacity")
public class FPCCapacityController extends BasicController<FPCCapacity> {
    private static final Logger logger = LoggerFactory.getLogger(FPCCapacityController.class);

    @Autowired
    FPCService mFPCService;


    public FPCCapacityController() {
        super(FPCCapacity.class);
    }

    /**
     * 条件查询
     */
    @GetMapping("/ProductAll")
    public Object All(HttpServletRequest request) {
        Object wResult;
        try {
            BMSEmployee wLoginUser = GetSession(request);

            Map<String, Object> wParams = GetParamsMap(request);
            Pagination wPagination = GetPagination(request);

            int wFactoryID = StringUtils.parseInt(request.getParameter("FactoryID"));
            int wWorkShopID = StringUtils.parseInt(request.getParameter("WorkshopID"));
            int wWorkAreaID = StringUtils.parseInt(request.getParameter("WorkAreaID"));
            int wStationID = StringUtils.parseInt(request.getParameter("StationID"));
            int wProductID = StringUtils.parseInt(request.getParameter("ProductID"));

            if (wProductID <= 0 || wFactoryID <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            if (wWorkAreaID > 0 && wWorkShopID <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }
            if (wStationID > 0 && wWorkAreaID <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<List<FPCCapacity>> wServiceResult =
                    mFPCService.FPC_CreateCapacityList(wLoginUser, wProductID, wFactoryID, wWorkShopID, wWorkAreaID, wStationID);


            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, wPagination.getTotalPage());
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

}
